package com.nari.ncps.screen.service.impl.coal;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nari.ncps.screen.entity.coal.CoalOpermonEquipFault;
import com.nari.ncps.screen.mapper.coal.CoalOpermonEquipFaultMapper;
import com.nari.ncps.screen.service.coal.CoalOpermonEquipFaultService;
import com.nari.ncps.screen.vo.coal.CoalEquipmentFaultVO;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDate;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 运行检测设备故障信息表 服务实现类
 * </p>
 *
 * @author xuesong
 * @since 2025-08-23
 */
@Service
public class CoalOpermonEquipFaultServiceImpl extends ServiceImpl<CoalOpermonEquipFaultMapper, CoalOpermonEquipFault> implements CoalOpermonEquipFaultService {

    @Override
    public CoalEquipmentFaultVO getEquipmentFaultStatistics(Integer year) {


        if (year == null) {
            year = LocalDate.now().getYear();
        }

        // 查询指定年份当前月份的数据
        int currentMonth = LocalDate.now().getMonthValue();
        LambdaQueryWrapper<CoalOpermonEquipFault> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(CoalOpermonEquipFault::getYear, year)
                .eq(CoalOpermonEquipFault::getMonth, currentMonth)
                .eq(CoalOpermonEquipFault::getOrgCode, 100360);

        CoalOpermonEquipFault result = this.getOne(wrapper);

        CoalEquipmentFaultVO coalEquipmentFaultVO = new CoalEquipmentFaultVO();
        if (result != null) {
            BigDecimal time = new BigDecimal(720);
            coalEquipmentFaultVO.setTransportVehicleFaultCount(result.getTransVehicleFaultCount());
            coalEquipmentFaultVO.setTransportVehicleProcessingTime(result.getTransVehicleFaultDur());
            coalEquipmentFaultVO.setTransportVehicleFaultRate(result.getTransVehicleFaultCount().divide(time,2,RoundingMode.HALF_UP));

            coalEquipmentFaultVO.setAuxiliaryEquipmentFaultCount(result.getAuxiliaryFaultCount());
            coalEquipmentFaultVO.setAuxiliaryEquipmentProcessingTime(result.getAuxiliaryFaultDur());
            coalEquipmentFaultVO.setAuxiliaryEquipmentFaultRate(result.getAuxiliaryFaultCount().divide(time,2,RoundingMode.HALF_UP));

            coalEquipmentFaultVO.setElectricShovelFaultCount(result.getElectricShovelFaultCount());
            coalEquipmentFaultVO.setElectricShovelProcessingTime(result.getElectricShovelFaultDur());
            coalEquipmentFaultVO.setElectricShovelFaultRate(result.getElectricShovelFaultCount().divide(time,2,RoundingMode.HALF_UP));

        }


        return coalEquipmentFaultVO;


//        // 如果未指定年份，使用当前年份
//        if (year == null) {
//            year = LocalDate.now().getYear();
//        }
//
//        // 查询指定年份当前月份的数据
//        int currentMonth = LocalDate.now().getMonthValue();
//        LambdaQueryWrapper<CoalOpermonEquipFault> wrapper = Wrappers.lambdaQuery();
//        wrapper.eq(CoalOpermonEquipFault::getYear, year)
//               .eq(CoalOpermonEquipFault::getMonth, currentMonth);
//
//        List<CoalOpermonEquipFault> faultList = this.list(wrapper);
//
//        // 初始化VO
//        CoalEquipmentFaultVO vo = new CoalEquipmentFaultVO();
//
//        // 统计数据
//        int truckFaultCount = 0;         // 运输车辆故障总数
//        int truckTotalTime = 0;          // 运输车辆总处理时长
//        BigDecimal truckFaultRate = BigDecimal.ZERO;  // 运输车辆故障率
//
//        int auxiliaryFaultCount = 0;     // 附属设备故障总数
//        int auxiliaryTotalTime = 0;      // 附属设备总处理时长
//        BigDecimal auxiliaryFaultRate = BigDecimal.ZERO;  // 附属设备故障率
//
//        int shovelFaultCount = 0;        // 电铲故障总数
//        int shovelTotalTime = 0;         // 电铲总处理时长
//        BigDecimal shovelFaultRate = BigDecimal.ZERO;  // 电铲故障率
//
//
//        // 按月份排序
//        List<CoalOpermonEquipFault> sortedList = faultList.stream()
//                .sorted((o1, o2) -> o1.getMonth().compareTo(o2.getMonth()))
//                .collect(Collectors.toList());
//
//
//        // 遍历数据进行统计
//        for (CoalOpermonEquipFault fault : sortedList) {
//            int type = fault.getEquipmentType() != null ? fault.getEquipmentType() : 0;
//            int count = fault.getFaultCount() != null ? fault.getFaultCount() : 0;
//            int time = fault.getProcessingTime() != null ? fault.getProcessingTime() : 0;
//            BigDecimal rate = fault.getFaultRate() != null ? fault.getFaultRate() : BigDecimal.ZERO;
//
//            if (type == 0) {
//                // 运输车辆
//                truckFaultCount += count;
//                truckTotalTime += time;
//                truckFaultRate = rate;  // 使用最新的故障率
//            } else if (type == 1) {
//                // 附属设备
//                auxiliaryFaultCount += count;
//                auxiliaryTotalTime += time;
//                auxiliaryFaultRate = rate;  // 使用最新的故障率
//            } else if (type == 2) {
//                // 电铲
//                shovelFaultCount += count;
//                shovelTotalTime += time;
//                shovelFaultRate = rate;  // 使用最新的故障率
//            }
//        }
//
//        // 设置运输车辆数据
//        vo.setTransportVehicleFaultCount(truckFaultCount);
//        vo.setTransportVehicleProcessingTime(truckTotalTime / Math.max(1, getTruckRecordCount(faultList)));
//        vo.setTransportVehicleFaultRate(truckFaultRate.setScale(2, RoundingMode.HALF_UP));
//
//        // 设置附属设备数据
//        vo.setAuxiliaryEquipmentFaultCount(auxiliaryFaultCount);
//        vo.setAuxiliaryEquipmentProcessingTime(auxiliaryTotalTime / Math.max(1, getAuxiliaryRecordCount(faultList)));
//        vo.setAuxiliaryEquipmentFaultRate(auxiliaryFaultRate.setScale(2, RoundingMode.HALF_UP));
//
//        // 设置电铲数据
//        vo.setElectricShovelFaultCount(shovelFaultCount);
//        vo.setElectricShovelProcessingTime(shovelTotalTime / Math.max(1, getShovelRecordCount(faultList)));
//        vo.setElectricShovelFaultRate(shovelFaultRate.setScale(2, RoundingMode.HALF_UP));
//
//
//
//
//        return vo;

    }
    
    /**
     * 获取运输车辆记录数
     */
    private int getTruckRecordCount(List<CoalOpermonEquipFault> faultList) {
        int count = 0;
        for (CoalOpermonEquipFault fault : faultList) {
            if (fault.getEquipmentType() != null && fault.getEquipmentType() == 0) {
                count++;
            }
        }
        return count;
    }
    
    /**
     * 获取附属设备记录数
     */
    private int getAuxiliaryRecordCount(List<CoalOpermonEquipFault> faultList) {
        int count = 0;
        for (CoalOpermonEquipFault fault : faultList) {
            if (fault.getEquipmentType() != null && fault.getEquipmentType() == 1) {
                count++;
            }
        }
        return count;
    }
    
    /**
     * 获取电铲记录数
     */
    private int getShovelRecordCount(List<CoalOpermonEquipFault> faultList) {
        int count = 0;
        for (CoalOpermonEquipFault fault : faultList) {
            if (fault.getEquipmentType() != null && fault.getEquipmentType() == 2) {
                count++;
            }
        }
        return count;
    }
}
